package com.sjq.common.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sjq.common.dao.AuthorizedDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.entity.Authorized;
import com.sjq.common.entity.Menu;
@Repository
public class AuthorizedDaoImpl extends GenericDao<Authorized> implements AuthorizedDao {

	@Override
	public List<Authorized> selectYSQ(String jsid) {
		// TODO Auto-generated method stub
		String sql="select JS_ID,XXZJBH from T_PZ_SYS_ROLE_MENU where js_id='"+jsid+"' ";
		List<Authorized> list = this.getJdbcTemplate().query(sql, new RowMapper<Authorized>(){

			@Override
			public Authorized mapRow(ResultSet rs, int index) throws SQLException {
				// TODO Auto-generated method stub
				Authorized authorized = new Authorized();
				
				authorized.setJs_id(rs.getString("JS_ID"));
				
				authorized.setJs_id(rs.getString("XXZJBH"));
				return authorized;
			}});
		return list;
	}

	
	
	@Override
	public int[] saveAuthorized(final JSONArray arr, String jsid, String yhm,String dwbm) {
		String sql="insert into T_PZ_SYS_ROLE_MENU (JS_ID,DJDW_GAJGJGDM,DJR_YHM,SJGSDWDM,SJGSRY,XXZJBH)values('"+jsid+"','"+dwbm+"','"+yhm+"','"+dwbm+"','"+yhm+"',?)";
		int[] i = this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				
				ps.setString(1, arr.getJSONObject(i).getString("menu_xxzjbh"));
			}
			
			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return arr.size();
			}
		});
		
		return i;
	}



	@Override
	public int deleteAuthorized(String jsid) {
		// TODO Auto-generated method stub
		
		String sql="delete T_PZ_SYS_ROLE_MENU where JS_ID='"+jsid+"'";
		int i = this.getJdbcTemplate().update(sql);
		return i;
	}



	@Override
	public List<Menu> getAuthorizedMenu(String jsid) {
		String sql=" select a.xxzjbh ,a.xt_id systemId,a.cd_id menuId,a.cdmc menuName,a.wjcclj menuUrl,a.cdcs,a.fcd_id parentMenuId,a.cdpx orderno,a.cdms,nvl2(b.XXZJBH,'true','false' ) checked,'false' nocheck "+
				" 	 from T_PZ_SYS_MENU a left join " +
				"		 (select * from T_PZ_SYS_ROLE_MENU where js_id='"+jsid+"' and djbgbs<>'3' ) b" +
				"   on a.xxzjbh= b.XXZJBH "+
				"  where a.djbgbs<>'3' order by a.cd_id ";
		
		return this.getJdbcTemplate().query(sql,new BeanPropertyRowMapper(Menu.class));
	}
}
